<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="/WEB-INF/views/common/header.jsp" %>
    <title>修改管理员</title>
</head>
<body>
<%@include file="/WEB-INF/views/common/top.jsp" %>
<%@include file="/WEB-INF/views/common/nav.jsp" %>

<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="show-top-grids">
        <div class="main-grids about-main-grids">
            <h4>修改管理员</h4>
            <div class="recommended-info" pane="">

                <hr class="layui-bg-green">
                <form id="createMasterForm" class="layui-form layui-form-pane" style="margin-top: 1em;" method="post"
                      action='<c:url value="/master/create"/>'>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName" id="userName" readonly="readonly" placeholder="用户名"
                                       autocomplete="off"
                                       value="${master.userName}" class="layui-input"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">登录名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="loginName" readonly="readonly" id="loginName"
                                       placeholder="登录名" autocomplete="off"
                                       value="${master.loginName}" class="layui-input"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-inline">
                                <select id="type" name="type" lay-verify="type" style="display: inline-block;"
                                        class="layui-input">
                                    <option value="">请选择</option>
                                    <c:forEach var="role" items="${roles}" varStatus="state">
                                        <c:if test="${role.id != 1}">
                                            <option
                                                    <c:if test="${master.type == role.id }">selected</c:if>
                                                    value="${role.id}">${role.roleName}</option>
                                        </c:if>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <input type="hidden" id="id" name="id" value="${master.id}"/>
                            <button class="layui-btn" type="submit" id="btn_create_master">修改</button>
                        </div>

                    </div>

                    <div id="divCheck" class="layui-form-item">
                        <label class="layui-form-label">权限预览</label>
                        <c:forEach var="func" items="${functions}" varStatus="status">
                            <div class="layui-col-xs12">
                                <div style="height: 50px;" class="layui-col-xs2">
                                    <input type="checkbox" name="functionIds" tag-index="${func.functionType}"
                                           lay-filter="func_type_${func.functionType}" value="${func.id}"
                                           lay-skin="primary"
                                           title="${func.functionName}">
                                </div>
                                <div class="layui-col-md9 layui-col-space1">
                                    <c:if test="${func.subFunctionInfoVo != null}">
                                        <c:forEach var="subFunc" items="${func.subFunctionInfoVo}"
                                                   varStatus="subStatus">
                                            <div class="layui-col-md12 layui-col-space1">
                                                <div style="height: 50px;" class="layui-col-md3 layui-col-space1">
                                                    <input type="checkbox" tag-index="${subFunc.functionType}"
                                                           name="functionIds" lay-skin="primary"
                                                           lay-submit="func_type_${subFunc.functionType}"
                                                           value="${subFunc.id}" title="${subFunc.functionName}">
                                                </div>

                                                <c:if test="${subFunc.subFunctionInfoVo != null}">
                                                    <div class="layui-col-md8">
                                                        <c:forEach var="finalFunc" items="${subFunc.subFunctionInfoVo}"
                                                                   varStatus="subStatus">
                                                            <div class="layui-col-md3" style="height: 50px;">
                                                                <input type="checkbox"
                                                                       tag-index="${finalFunc.functionType}"
                                                                       name="functionIds"
                                                                       lay-skin="primary" value="${finalFunc.id}"
                                                                       lay-submit="func_type_${finalFunc.functionType}"
                                                                       title="${finalFunc.functionName}">
                                                            </div>
                                                        </c:forEach>
                                                    </div>
                                                    <hr class="layui-bg-gray">
                                                </c:if>
                                            </div>
                                        </c:forEach>
                                    </c:if>
                                </div>
                            </div>

                        </c:forEach>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>
</body>

<script type="application/javascript">


    layui.use('layer', function () {
        window.layer = layui.layer;
    });


    /*导航栏参数*/
    var $nav_a = $("#ul_nav a[href='/master/list']");
    $nav_a.parent().addClass("active");
    $nav_a.parent().parent().css("display", "block");

    $(function () {

        /*修改checkbox 样式*/
        $("#divCheck :checkbox").each(function () {
            var title = $(this).attr("title");
            var funcType = $(this).attr("tag-index");
            var div = $("<div class=\"layui-unselect layui-form-checkbox\" func-type=" + funcType + " lay-skin=\"primary\">"
                + "<span>" + title + "</span><i class=\"layui-icon\"></i></div>");
            $(this).after(div);
        });

        /*用户类型选择预览对应权限*/
        $("#type").change(function () {
            var roleId = $(this).val();
            $(":checkbox[name='functionIds']").next().removeClass("layui-form-checked");
            if (roleId == "") {
                return true;
            }
            initFunctions(roleId);
        });


        /*验证信息*/
        window.fault = false;
        $("#createMasterForm").validate({
            onfocusout: false,
            onkeyup: false,
            onclick: false,
            submitHandler: function () {
                var formData = $('#createMasterForm').serializeObject();
                var password = formData["password"] || '';
                formData["password"] = $.md5(password);
                formData = JSON.stringify(formData);
                $.ajax({
                    url: '<c:url value="/master/update"/>',
                    dataType: 'json',
                    type: 'post',
                    data: formData,
                    contentType: "application/json",
                    error: function () {
                    },
                    success: function (data) {
                        if (data.code !== "200") {
                            window.layer.msg(data.message, {icon: 5, anim: 6});
                        } else {
                            window.location.href = '<c:url value="/master/list"/>';
                        }
                    }
                });
            },
            rules: {
                type: "required"
            },
            messages: {
                type: "请选择类型"

            },
            showErrors: function (errorMap, errorList) {
                window.fault = this.numberOfInvalids() > 0;
                this.defaultShowErrors();
            },
            errorPlacement: function (error, element) {
                if (window.fault) {
                    window.fault = false;
                    $(element).addClass("layui-form-danger").focus();
                    window.layer.msg($(error).text(), {icon: 5, anim: 6});
                }
            }
        });


        initFunctions(${master.type});

    });


    function initFunctions(roleId) {
        roleId = roleId || "";
        if (roleId == "") {
            return true;
        }
        $.ajax({
            url: '<c:url value="/function/queryByRole"/>',
            dataType: 'json',
            type: 'post',
            data: {roleId: roleId},
            error: function () {
            },
            success: function (data) {
                if (data.code !== "200") {
                    window.layer.msg(data.message, {icon: 5, anim: 6});
                } else {
                    $.each(data.data, function (i, o) {
                        $(":checkbox[value='" + o.id + "']").next().addClass("layui-form-checked");
                    });
                }
            }
        });
    }
</script>


</html>
